<template>
    <div class="invitation">
        <div class="header">{{ $t('msg.邀请') }}</div>
        <div class="content">
            <div class="lemon-sidebar__scroll list">
                <div class="item" v-for="(item, index) in list" :key="index" @click="submit(item)">
                    <img :src="item.avatar" alt="" class="avatar">
                    <div class="name">{{ item.nickname }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState } from "vuex";
export default {
    data() {
        return {
            page: 1,
            last_page: 1,
            list: [],
            total: 0
        }
    },
    computed: {
        // ...mapState(['contactKeywords'])
        ...mapState({
            contactKeywords: state => state.contactKeywords,
            allContacts: state => state.allContacts
        })
    },
    watch: {
        contactKeywords: {
            handler(newVal) {
                this.keywords = newVal;
                this.getList();
            },
            immediate: true
        }
    },
    methods: {
        async getList() {
            let { list: res } = await this.$api.usersApi.inviteList({
                page: this.page,
                keyword: this.keywords
            });
            this.last_page = res.last_page;
            this.list = this.page == 1 ? res.data : [...this.list, ...res.data];
        },
        loadmore() {
            if (this.last_page == this.page) return;
            this.page++;
            this.getList();
        },
        async submit(item) {
            let user_id = item.id;
            let { user } = await this.$api.chatApi.searchUser({
                user_id
            });
            let im_id = user.im_user_id;
            let contact = this.allContacts.find(item => item.user_id == im_id);
             this.$store.commit('changeChatUser', contact);
            setTimeout(() => {
                this.$router.replace({
                    name: 'message'
                })
            }, 100);
        }
    }
}
</script>
<style lang="scss" scoped>
.invitation {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #fff;

    .header {
        height: 92px;
        line-height: 92px;
        font-size: 20px;
        color: #333333;
        padding: 0 24px;
        box-sizing: border-box;
        background: #FCFCFE;
        box-shadow: inset 0px -1px 0px 0px rgba(104, 104, 104, 0.1);
    }

    .content {
        flex: 1;
        display: flex;
        overflow: hidden;

        .list {
            width: 430px;
            cursor: default;
            overflow-y: auto;

            .item {
                height: 88px;
                padding: 10px 24px;
                box-sizing: border-box;
                display: flex;
                align-items: center;

                &.active {
                    background-color: #D9E7FE;
                }

                &:hover:not(&.active) {
                    background: #e3e3e3
                }

                .avatar {
                    width: 52px;
                    height: 52px;
                    border-radius: 4px 4px 4px 4px;
                }

                .avatar {
                    width: 52px;
                    height: 52px;
                    border-radius: 4px 4px 4px 4px;
                }

                .name {
                    flex: 1;
                    padding-left: 12px;
                    box-sizing: border-box;
                    font-size: 18px;
                    color: #333333;

                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
            }
        }
    }
}
</style>